import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Table, Tag, Space, Button, Popconfirm } from 'antd';
import action from "../../../store/action";
import { Link } from 'react-router-dom';
import { user_table } from '../../view_public/Add_public';
import { passCation } from '../../../component/Notification';
import { withRouter } from 'react-router-dom'

class index extends React.Component {
    constructor() {
        super();

    }
    componentWillMount() {
        this.handel_manageGet();
        const { management_delete } = this.props;
        management_delete()
    }
    render() {
        const columns = [
            {
                title: '用户名',
                dataIndex: 'lname',
                key: 'lname',
                render: text => <a>{text}</a>,
                width: 150
            },
            {
                title: '姓名',
                dataIndex: 'name',
                key: 'name',
                width: 80

            },
            {
                title: '性别',
                dataIndex: 'sex',
                key: 'sex',
                width: 80,
                render: (text, record) => text === 1 ? '男' : '女'

            },
            {
                title: '用户类型',
                dataIndex: 'usertype',
                key: 'usertype',
                width: 100,
                render: (text, record) => text === 1 ? '管理员' : '普通用户'

            },
            {
                title: '单位',
                key: 'organizationCode',
                dataIndex: 'organizationCode',
                width: 150
            },
            {
                title: '专业方向',
                key: 'professionalDirection',
                dataIndex: 'professionalDirection',
                width: 150
            },
            {
                title: '职称',
                key: 'titleOfTechnicalPost',
                dataIndex: 'titleOfTechnicalPost',
                width: 150
            },
            {
                title: '邮箱',
                key: 'mail',
                dataIndex: 'mail',
                width: 200
            },
            {
                title: '创建日期',
                key: 'createTime',
                dataIndex: 'createTime',
                width: 150
            },
            {
                title: '操作',
                key: 'action',
                width: 200,
                render: (text, record, index) => {
                    return <Space size="middle">
                        <Button type='link' onClick={() => this.handel_edit(record)}>编辑</Button>
                        {/* <Button type='link' onClick={() => this.handel_edit(record)}>编辑</Button> */}
                        <Popconfirm
                            title="确定要重置密码 123456吗？"
                            okText="确定"
                            cancelText="取消"
                            onConfirm={() => this.handleDelete(record)}
                        >
                            <Button type="link"  style={{ padding: "0" }}>
                                重置密码
                          </Button>
                        </Popconfirm>
                        <Popconfirm
                            title="确定要删除该用户吗？"
                            okText="确定"
                            cancelText="取消"
                            onConfirm={() => this.Delete_admin(record)}
                        >
                            <Button type='link' danger>删除</Button>
                        </Popconfirm>
                    </Space>
                },
            },
        ];
        return <div>
            {
                user_table(columns, this.props.manage_data)
            }
        </div>
    }
    Delete_admin=(e)=>{
        
        this.$axios.get(`/manager/api/deleteUser/${e.code}`).then((res) => {
            if(res.status===1){
                this.handel_manageGet();
                passCation('删除成功!')
            }
        }).catch((err) => {
            console.log(err);
        })
    }
    handel_manageGet = async () => {
        let newName = sessionStorage.getItem('admin_key');
       await this.$axios.get(`/manager/api/pageGetUserList/0/9999?lUserCode=${newName}`).then((res) => {
            const { management_get } = this.props;
            let list = {};
            [].push.apply(list, res.userList)
            management_get(list)
        }).catch((err) => {
            console.log(err);
        })
    }
    handleDelete = (e) => {
        this.$axios.get(`/manager/api/restPwd/${e.code}`).then((res) => {
            passCation('重置成功!')
        }).catch((err) => {
            console.log(err);
        })
    }
    handel_edit = (e) => {
        const { management_edit } = this.props;
        management_edit(e);
        this.$axios.get(`/manager/api/getUserPermissionsOrganization/${e.code}`).then((res) => {
            const { management_editSave } = this.props;
            let new_List = {};
            let second_list = {};
            [].push.apply(new_List, res.notSelectorganizationList);
            [].push.apply(second_list, res.selectOrganizationList);
            management_editSave(new_List, second_list)
            this.props.history.push('/admin/Modal')
        }).catch((err) => {
            console.log(err);
        })
    }
}

export default withRouter(connect((state) => ({ ...state.custom }), action.custom)(index))